import { create } from 'zustand';
import { devtools, persist, subscribeWithSelector } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';

import { createAgentsSettingsSlice } from './slices/settings';
import type { AgentsSettingsSlice } from './slices/settings';

// Agents Store类型
export type AgentsStore = AgentsSettingsSlice;

// 创建agents store
export const useAgentsStore = create<AgentsStore>()(
  devtools(
    persist(
      subscribeWithSelector(
        immer((set, get, store) => ({
          ...createAgentsSettingsSlice(set, get, store),
        }))
      ),
      {
        name: 'agents-store',
        // 指定需要持久化的状态
        partialize: (state) => ({
          webSearchEnabled: state.webSearchEnabled,
          webSearchSettings: state.webSearchSettings,
          selectedFiles: state.selectedFiles,
          maxSelectedFiles: state.maxSelectedFiles,
          settings: state.settings,
          settingsVersion: state.settingsVersion,
        }),
        version: 1,
      }
    ),
    {
      name: 'AgentsStore',
    }
  )
);

// 导出类型
export type { AgentsSettingsSlice };
export type { FileInfo, WebSearchSettings, AgentsSettings } from './slices/settings';
